package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.AccountProperty;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.AccountPropertyRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AccountPropertyService {
    @Autowired
    AccountPropertyRepository accountPropertyRepository;

    @Cacheable(value = "projectPage")
    public Page<AccountProperty> findAllAccoutProperty(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<AccountProperty> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<AccountProperty> page = accountPropertyRepository.findAll(mySpecifications, pageable);
        return page;
    }
    public Page<AccountProperty> findAllAccoutProperty(int pageNum, HttpServletRequest request,int pageSize) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<AccountProperty> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
        Page<AccountProperty> page = accountPropertyRepository.findAll(mySpecifications, pageable);
        return page;
    }


    //保存
    public void saveAccountPropertyInfo(String id, String code, String name, Integer objName, Integer parameterType,Integer propertyType, Integer isModify) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        AccountProperty accountProperty = new AccountProperty();
        String uuid = "";
        if (id != null) {
            accountProperty = findById(id);
            accountProperty.setCreateUser(new User(shiroUser.getId()));
            accountProperty.setUpdateTime(new Date());
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            accountProperty.setId(uuid);
            accountProperty.setCreateTime(date);
            accountProperty.setCreateUser(new User(shiroUser.getId()));
            accountProperty.setIsValid(1);
        }

        accountProperty.setCode(code);
        accountProperty.setName(name);
        accountProperty.setObjName(objName);
        accountProperty.setParameterType(parameterType);
        accountProperty.setPropertyType(propertyType);
        if(isModify == null){
            isModify = 0;
        }
        accountProperty.setIsModify(isModify);

        accountPropertyRepository.save(accountProperty);
    }

    //编辑框回显数据
    public AccountProperty findById(String id) {
        AccountProperty code = accountPropertyRepository.findOne(id);
        return code;
    }
    // 删除
    public void delAccountPropertyInfo(String[] ids) {
        List<AccountProperty> parameterInfos = accountPropertyRepository.findAll(Arrays.asList(ids));
        for (AccountProperty accountProperty:parameterInfos) {
            accountProperty.setIsValid(0);
        }
        accountPropertyRepository.save(parameterInfos);
    }
    //导出
    public List<AccountProperty> findAllAccoutPropertyInfo() {
        return accountPropertyRepository.findByIsValid();
    }
    //导入
    public String saveImport(List<String[]> list,String flag) {
        List<AccountProperty> allCode = accountPropertyRepository.findCodes();
        flag = "0";
        for (String[] info : list) {
            if(allCode.contains(info[0])){
                return flag = "1";
            }
            AccountProperty ainfo = new AccountProperty();
            ainfo.setCode(info[0]);
            ainfo.setName(info[1]);
            ainfo.setObjName(Integer.valueOf(info[2]));
            ainfo.setParameterType(Integer.valueOf(info[3]));
            ainfo.setPropertyType(Integer.valueOf(info[4]));
            ainfo.setIsModify(Integer.valueOf(info[5]));

            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            ainfo.setId(uuid);
            ainfo.setCreateTime(new Date());
            ainfo.setCreateUser(new User(shiroUser.getId()));
            ainfo.setIsValid(1);
            accountPropertyRepository.save(ainfo);
        }
        return flag;
    }
    //校验code
    public List<AccountProperty> findCode() {
        List<AccountProperty> codes = accountPropertyRepository.findCodes();
        return codes;
    }
}
